package com.offer;


import com.bean.ListNode;
import com.utils.DataUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Test52 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }

        ListNode a = headA, b = headB;

        while (a != b) {
            if (a != null) {
                a = a.next;
            } else {
                a = headB;
            }

            if (b != null) {
                b = b.next;
            } else {
                b = headA;
            }
        }

        return a;
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(4, new ListNode(1));
        ListNode b = new ListNode(5, new ListNode(0, new ListNode(1)));

        ListNode common = new ListNode(8, new ListNode(4, new ListNode(5)));

        a.next.next = common;
        b.next.next.next = common;

        Test52 test = new Test52();
        System.out.println(test.getIntersectionNode(a, b).val);
//        DataUtils.printListNode(test.reserveList(a));
    }

}
